import javax.swing.*;
import java.util.logging.Logger;
import java.io.*;

public class StudentMainClass extends JFrame{
    
    protected StudentFileReader studFileReader;
        protected StudentGUI studGUI;

        protected static Student stud[];
        protected static Object data[][];


        //Runs all classes connected with StudentMainClass
        
        public void run() throws Exception{
                studFileReader = new StudentFileReader();
                setStudents();

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        studGUI = new StudentGUI();
                    }
                });

                return;
        }


    public static void main(String[] args) {
        try{
                //StudentMainClass studProg = new StudentMainClass();
                //studProg.run();
            
		MemberType s=new MemberType();

		s.setVisible(true);
		s.setSize(270,180);
		s.setLocation(500,250);
        }

        catch(Exception e){
                System.out.println(e);
        }
    }



        private void setStudents(){
                data = new Object[stud.length][5];

                for(int i = 0; i < stud.length; i++){
                data[i] = new Object[]{getID(i),
                                                           getName(i),
                                                           getCourse(i),
                                                           getYear(i),
                                                           getSchedule(i),
                                                           getClassCode(i)};
                }
        }

        

        protected static void saveData(){
                Student[] newStud = new Student[StudentGUI.tableStudList.getRowCount()];
                String updatedfile = "students_updated.dat";

                for(int i=0; i < StudentGUI.tableStudList.getRowCount(); i++){
                        newStud[i] = new Student(getTableID(i), getTableName(i), getTableCourse(i), getTableYear(i), getTableClassCode(i), getTableSchedule(i));
                }

                try{
                        ObjectOutput objOut = new ObjectOutputStream(new FileOutputStream(updatedfile));
                        objOut.writeObject(newStud);
                        objOut.close();
                }

                catch(Exception e){
                        System.out.println(e);
                }
        }

        //Method for setting ButtonDialog fields

        protected static void setFields(int i){
              //  ButtonDialog.txtID.setEditable(false);
                setFieldID(i);
                setFieldName(i);
                setFieldCourse(i);
                setFieldYear(i);
                setFieldClassCode(i);
                setFieldSchedule(i);
                
        }

        //Method that updates the table with set values in ButtonDialog...

        protected static void updateTable(int r){
                setTableID(r);
                setTableName(r);
                setTableCourse(r);
                setTableYear(r);
                setTableClassCode(r);
                setTableSchedule(r);
                
        }

        //Setter methods for JTextFields in ButtonDialog class...

        private static void setFieldID(int i){
                ButtonDialog.txtID.setText(Integer.toString(getTableID(i)));
        }

        private static void setFieldName(int i){
                ButtonDialog.txtName.setText(getTableName(i));
        }

        private static void setFieldCourse(int i){
                ButtonDialog.txtCourse.setText(getTableCourse(i));
        }

        private static void setFieldYear(int i){
                ButtonDialog.txtYear.setText(Integer.toString(getTableYear(i)));
        }

        private static void setFieldClassCode(int i){
                ButtonDialog.txtClassCode.setText(getTableClassCode(i));
        }
        private static void setFieldSchedule(int i){
                ButtonDialog.txtSchedule.setText(getTableSchedule(i));
        }
        
        

        //Setter methods for JTable in StudentGUI class

        private static void setTableID(int r){
                StudentGUI.tableStudList.setValueAt(getFieldID(),r,0);
        }

        private static void setTableName(int r){
                StudentGUI.tableStudList.setValueAt(getFieldName(),r,1);
        }

        private static void setTableCourse(int r){
                StudentGUI.tableStudList.setValueAt(getFieldCourse(),r,2);
        }

        private static void setTableYear(int r){
                StudentGUI.tableStudList.setValueAt(getFieldYear(),r,3);
        }

        private static void setTableClassCode(int r){
                StudentGUI.tableStudList.setValueAt(getFieldClassCode(),r,4);
        }
        private static void setTableSchedule(int r){
                StudentGUI.tableStudList.setValueAt(getFieldSchedule(),r,5);
        }
        


        //Getter methods of stud[]


        private static int getID(int i){
                return stud[i].getID();
        }

        private static String getName(int i){
                return stud[i].getName();
        }

        private static String getCourse(int i){
                return stud[i].getCourse();
        }

        private static int getYear(int i){
                return stud[i].getYear();
        }

        private static String getClassCode(int i){
                return stud[i].getClassCode();
        }
        private static String getSchedule(int i){
                return stud[i].getSchedule();
        }
        



        // Getter methods for JTextField values in ButtonDialog

        private static int getFieldID(){
                return Integer.parseInt(ButtonDialog.txtID.getText());
        }

        private static String getFieldName(){
                return ButtonDialog.txtName.getText();
        }

        private static String getFieldCourse(){
                return ButtonDialog.txtCourse.getText();
        }

        private static int getFieldYear(){
                return Integer.parseInt(ButtonDialog.txtYear.getText());
        }

        private static String getFieldClassCode(){
                return ButtonDialog.txtClassCode.getText();
        }
        private static String getFieldSchedule(){
                return ButtonDialog.txtSchedule.getText();
        }
        


        //Getter methods for JTable in StudentGUI class


        private static int getTableID(int r){
                return Integer.parseInt(StudentGUI.tableStudList.getValueAt(r,0).toString());
        }

        private static String getTableName(int r){
                return StudentGUI.tableStudList.getValueAt(r,1).toString();
        }

        private static String getTableCourse(int r){
                return StudentGUI.tableStudList.getValueAt(r,2).toString();
        }

        private static int getTableYear(int r){
                return Integer.parseInt(StudentGUI.tableStudList.getValueAt(r,3).toString());
        }
        
        private static String getTableClassCode(int r){
                return StudentGUI.tableStudList.getValueAt(r,4).toString();
        }

        private static String getTableSchedule(int r){
                return StudentGUI.tableStudList.getValueAt(r,5).toString();
        }
        
        

}
